<?php

declare(strict_types=1);

namespace App\Model\Act;

use Hyperf\DbConnection\Model\Model;

/**
 * 限时充值活动
 */
class ActRechargeLimit extends Model
{
    protected $table = 'act_recharge_limit';
    protected $fillable = ['user_id', 'flag', 'recharge_rmb', 'recharge_coin', 'type'];

    static function addRecord($userId = 0, $rmb = 0, $coin = 0, $type = 1)
    {
        $item = self::getItem($userId, $type);
        if ($item) {
            //新手活动参加过
            if ($type == 1) return false;
            $checkToday = strtotime((string)$item->created_at) > strtotime(date('Y-m-d'));
            if ($checkToday) return false;
        }
        return self::create([
            'user_id' => $userId,
            'recharge_rmb' => intval($rmb),
            'recharge_coin' => intval($coin),
            'type' => $type,
            'flag' => 1
        ]);
    }

    static function getItem($userId, $type = 0)
    {
        $where = [
            'user_id' => $userId,
            'flag' => 1,
        ];
        $type > 0 && $where['type'] = $type;
        return self::where($where)->orderBy('id', 'desc')->first();
    }

    static function getBeginTime($userId, $type)
    {
        $item = self::getItem($userId, $type);
        return !$item ? 0 : strtotime((string)$item->created_at);
    }
}
